<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="studySubjectList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="科目名称" align="center" prop="title" />
      <el-table-column label="学习模式" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.children.length == 0">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleExam(scope.row, '1')"
          >顺序练习</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleExam(scope.row, '2')"
          >随机练习</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleExam(scope.row, '3')"
          >模拟考试</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改资源对话框 -->
    <el-dialog :title="exam.title + ' 题库练习'" :visible.sync="exam.open" v-if="exam.subjectId" width="800px" append-to-body>
      <order-exam :subjectId="exam.subjectId" v-if="exam.mode == '1'"></order-exam>
      <rand-exam :subjectId="exam.subjectId" v-if="exam.mode == '2'"></rand-exam>
      <mock-exam :subjectId="exam.subjectId" v-if="exam.mode == '3'"></mock-exam>
    </el-dialog>
  </div>
</template>

<script>
import { listStudySubject, getStudySubject, delStudySubject, addStudySubject, updateStudySubject, exportStudySubject } from "@/api/study/studySubject";
import MockExam from "@/components/Exam/Mock.vue";
import OrderExam from "@/components/Exam/Order.vue";
import RandExam from "@/components/Exam/Rand.vue";

export default {
  name: "MyStudyQuestion",
  components: {
    MockExam, OrderExam, RandExam
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 科目表格数据
      studySubjectList: [],
      // 学习练习
      exam: {
        // 弹出层标题
        title: "",
        open: false,
        subjectId: null,
        mode: null,
      },
      // 查询参数
      queryParams: {
        title: null,
        pid: null,
        status: '1'
      },
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询科目列表 */
    getList() {
      this.loading = true;
      listStudySubject(this.queryParams).then(response => {
        this.studySubjectList = this.handleTree(response.data, "id", "pid");
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 练习按钮操作 */
    handleExam(row, mode) {
      this.exam.subjectId = row.id;
      this.exam.title = row.title;
      this.exam.mode = mode;
      this.exam.open = true;
    }
  }
};
</script>
